package com.example.blackbox.DataBase;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.blackbox.Data.News;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class OpDataBase {
    private DataBase dataBase;
    private SQLiteDatabase sqLiteDatabase;
    private ArrayList<News> news=new ArrayList<>();

    public OpDataBase(Activity activity) {
        dataBase=new DataBase(activity,"news.db",null,1);
        sqLiteDatabase=dataBase.getWritableDatabase();
    }

    public void add(News data){
        ContentValues contentValues=new ContentValues();
        contentValues.put("newsTitle",data.getNewsTitle());
        contentValues.put("newsTag",data.getNewsTag());
        contentValues.put("newsMessage",data.getNewsMessage());
        contentValues.put("newsDate",data.getNewsDate());
        contentValues.put("imgUrl",data.getImgUrl());
        contentValues.put("level",data.getLevel());
        contentValues.put("author",data.getAuthor());
        contentValues.put("authorHead",data.getAuthorHead());
        contentValues.put("info",data.getInfo());

        sqLiteDatabase.insert("news",null,contentValues);
        Log.d("test","成了");
    }

    public ArrayList<News> findAllData(){
        news.clear();

        Cursor cursor=sqLiteDatabase.query("news",null,null,null,null,null,null,null);
        while (cursor.moveToNext()){
            String newsTitle =cursor.getString(cursor.getColumnIndex("newsTitle"));
            String newsTag =cursor.getString(cursor.getColumnIndex("newsTag"));
            String newsMessage=cursor.getString(cursor.getColumnIndex("newsMessage"));
            String newsDate=cursor.getString(cursor.getColumnIndex("newsDate"));

            String imgUrl = cursor.getString(cursor.getColumnIndex("imgUrl"));
            String level =cursor.getString(cursor.getColumnIndex("level"));
            String author=cursor.getString(cursor.getColumnIndex("author"));
            String authorHead=cursor.getString(cursor.getColumnIndex("authorHead"));
            String info=cursor.getString(cursor.getColumnIndex("info"));

            news.add(new News(newsTitle,newsTag,newsMessage,newsDate,imgUrl,level,author,authorHead,info));
        }
        return news;
    }

    public void deleteData(News news){
        sqLiteDatabase.delete("news","newsTitle = ?",new String[]{news.getNewsTitle()});
    }


}
